﻿@using Microsoft.AspNetCore.Components.Routing
@using System.Linq
@using Project.Constraints.Store.Models
@using Project.Web.Shared.Components
@if (!HasChildren)
{
    <MenuItem RouterLink="@Current.RouteUrl" RouterMatch="NavLinkMatch.All" Title="@RouterStore.GetLocalizerString(Route)">
        <IconTemplate>
            <MIcon ClassName="nav-icon" IconName="@Current.Icon"></MIcon>
        </IconTemplate>
    </MenuItem>
}
else
{
    <SubMenu>
        <TitleTemplate>
            <span class="subtitle_template">
                <Icon>
                    <Component>
                        <MIcon ClassName="nav-icon" IconName="@Route.Icon"></MIcon>
                    </Component>
                </Icon>
                <span>@RouterStore.GetLocalizerString(Route)</span>
            </span>
        </TitleTemplate>
        <ChildContent>
            @foreach (var item in Children)
            {
                <AntMenuItem Route="@item"></AntMenuItem>
            }
        </ChildContent>
    </SubMenu>
}


@code {
    [Parameter, NotNull] public RouteMenu? Route { get; set; }

    [Inject, NotNull] IRouterStore? RouterStore { get; set; }

    public RouteMenu Current => Route;
    // public RouteMenu Current => onlyOneChildren ? Children.First() : Route;

    // private bool onlyOneChildren => Children.Count() == 1;

    private bool HasChildren => Children.Any();

    private IEnumerable<RouteMenu> Children => RouterStore.Menus.Where(m => m.Group == Route.RouteId);
}
